package com.youlu.campus.web.config.filter;

/**
 * 不需要鉴权的  url 路径 实体
 *
 * @author su
 * @Description AuthExcludedUrl
 * @Date 2025/7/21
 * @module AuthExcludedUrl
 */
public class AuthExcludedUrl {

    private static final String EXCLUDED_TAG = "/*";

    /**
     * 相等
     */
    public static final String EQUALS = "equals";
    /**
     * 前缀
     */
    public static final String STARTWITH = "startWith";

    /**
     * 后缀
     */
    public static final String ENDWITH = "endWith";

    /**
     * 包含
     */
    public static final String CONTAINS = "contains";


    /**
     * 需要鉴权的  url
     */
    private final String checkUrl;

    /**
     * equal  or startWith with
     */
    private final String checkMehtod;

    public AuthExcludedUrl(String checkUrl, String checkMehtod) {
        this.checkUrl = checkUrl;
        this.checkMehtod = checkMehtod;

    }

    public static AuthExcludedUrl of(String checkUrl) {
        String checkMehtod;
        if (checkUrl.contains(EXCLUDED_TAG)) {
            if (checkUrl.endsWith(EXCLUDED_TAG) && checkUrl.startsWith(EXCLUDED_TAG)) {
                checkMehtod = AuthExcludedUrl.CONTAINS;
            } else if (checkUrl.endsWith(EXCLUDED_TAG)) {
                checkMehtod = AuthExcludedUrl.ENDWITH;
            } else {
                checkMehtod = AuthExcludedUrl.STARTWITH;
            }
        } else {
            checkMehtod = AuthExcludedUrl.EQUALS;
        }
        checkUrl = checkUrl.replaceAll("/\\*+", "");

        return new AuthExcludedUrl(checkUrl, checkMehtod);
    }

    /**
     * 是否排除
     *
     * @param url
     * @return
     */
    public boolean isExclude(String url) {
        if (AuthExcludedUrl.EQUALS.equals(checkMehtod)) {
            return url.equals(checkUrl);
        } else if (AuthExcludedUrl.STARTWITH.equals(checkMehtod)) {
            return url.startsWith(checkUrl);
        } else if (AuthExcludedUrl.ENDWITH.equals(checkMehtod)) {
            return url.endsWith(checkUrl);
        } else if (AuthExcludedUrl.CONTAINS.equals(checkMehtod)) {
            return url.contains(checkUrl);
        } else {
            return false;
        }
    }
}
